//
//  FeedbackViewController.m
//  AipuTel
//
//  Created by cretve on 15/1/15.
//  Copyright (c) 2015年 aipu. All rights reserved.
//

#import "FeedbackViewController.h"
#import <GCPlaceholderTextView/GCPlaceholderTextView.h>

@interface FeedbackViewController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet GCPlaceholderTextView *textView;

@end

@implementation FeedbackViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"意见反馈";
    
    _textView.placeholder = @"请输入建议内容";
    
    __weak FeedbackViewController* weakSelf = self;
    UITapGestureRecognizer* tgr = [UITapGestureRecognizer bk_recognizerWithHandler:^(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint location) {
        [weakSelf.phoneField resignFirstResponder];
        [weakSelf.textView resignFirstResponder];
    }];
    [_textView.superview.superview addGestureRecognizer:tgr];
    
    NSDictionary* userInfo = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"UsreInfo"];
    _phoneField.text = userInfo[@"username"];
}

- (IBAction)clickedSubmit:(id)sender
{
    if(_phoneField.text.length != 11)
    {
        [UIAlertView bk_showAlertViewWithTitle:@"请输入正确的手机号" message:nil cancelButtonTitle:@"好" otherButtonTitles:nil handler:nil];
        return;
    }
    
    if(_textView.text.length == 0)
    {
        [UIAlertView bk_showAlertViewWithTitle:@"请输入反馈内容" message:nil cancelButtonTitle:@"好" otherButtonTitles:nil handler:nil];
        return;
    }
    
    [_phoneField resignFirstResponder];
    [_textView resignFirstResponder];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"请稍候...";
    
    __weak FeedbackViewController* weakSelf = self;
    [[AFHTTPRequestOperationManager manager] POST:@"http://202.98.133.196/ddserver/submitfeedback" parameters:@{@"phone": _phoneField.text, @"feedback": _textView.text} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
        
        if([responseObject[@"errno"] integerValue] == 0)
        {
            [UIAlertView bk_showAlertViewWithTitle:@"提交成功" message:nil cancelButtonTitle:@"好" otherButtonTitles:nil handler:nil];
            
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            [UIAlertView bk_showAlertViewWithTitle:responseObject[@"info"] message:nil cancelButtonTitle:@"好" otherButtonTitles:nil handler:nil];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
        [UIAlertView bk_showAlertViewWithTitle:NetworkErrorMessage message:nil cancelButtonTitle:@"好" otherButtonTitles:nil handler:nil];
    }];
}

@end
